package leetcode.top100;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 * TODO 方式2：
 * @date 2020/4/1 23:12
 */
public class Code46_全排列 {
    public List<List<Integer>> permute(int[] nums) {
        if(nums == null || nums.length == 0)return new LinkedList<>();
        List<List<Integer>> res = new LinkedList<>();
        process(nums,0,res);
        return res;
    }

    private void process(int[] nums, int index,  List<List<Integer>> res) {
        if(index >= nums.length){
            List<Integer> temp = new LinkedList<>();
            for (int num : nums) {
                temp.add(num);
            }
            res.add(temp);
            return;
        }
        for (int i = index; i < nums.length; i++) {
            swap(nums,i,index);
            process(nums,index+1,res);
            swap(nums,index,i);
        }
    }

    private void swap(int[] nums, int i, int j) {
        if(i != j){
            nums[i] = nums[i] ^ nums[j];
            nums[j] = nums[i] ^ nums[j];
            nums[i] = nums[i] ^ nums[j];
        }
    }
}
